//
//  TaskViewController.m
//  NoneCar
//
//  Created by DongSen on 2018/7/10.
//  Copyright © 2018年 董森森. All rights reserved.
//

#import "SearchViewController.h"
#import "TaskHeadView.h"
#import "TaskTableViewCell.h"
#import "TaskViewController.h"
#import "PopSortingView.h"
#import "PopScreeningView.h"
@interface TaskViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property(nonatomic,strong)NSMutableArray * dataArray;
@property(nonatomic,strong)TaskHeadView * headView;
@property(nonatomic,strong)PopSortingView * sortView;
@property(nonatomic,strong)PopScreeningView * popScreenView;

@property (weak, nonatomic) IBOutlet UIView *screenView;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *topToView;

@end

@implementation TaskViewController

-(TaskHeadView *)headView{
    if (!_headView) {
        _headView = [TaskHeadView loadView];
        _headView.frame = CGRectMake(0, 0, kSCREEN_W, 40);
        [_headView.sortingButton addTarget:self action:@selector(sortingButton) forControlEvents:UIControlEventTouchUpInside];
        [_headView.screeningButton addTarget:self action:@selector(screeningButton) forControlEvents:UIControlEventTouchUpInside];
        [self.screenView addSubview:_headView];
    }
    return _headView;
}
-(PopSortingView *)sortView{
    if (!_sortView) {
        _sortView = [PopSortingView loadView];
        WeakSelf(self);
        _sortView.frame = CGRectMake(0, kNAVIGATIONBARHEIGHT+40, 375, kSCREEN_H-kNAVIGATIONBARHEIGHT-40);
        _sortView.block = ^(NSString *str){
            weakSelf.headView.sortString = str;
        };
        _sortView.disblock = ^{
            weakSelf.headView.sortShowing = NO;
        };
    }
    return _sortView;
}
- (PopScreeningView *)popScreenView{
    if (!_popScreenView) {
        _popScreenView = [PopScreeningView loadView];
        _popScreenView.frame = CGRectMake(0, kNAVIGATIONBARHEIGHT+40, 375, kSCREEN_H-kNAVIGATIONBARHEIGHT-40);
        WeakSelf(self);
        _popScreenView.block = ^(NSString *str){
            weakSelf.headView.screenString = str;
        };
        _popScreenView.disblock = ^{
            weakSelf.headView.screenShowing = NO;
        };
    }

    return _popScreenView;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationController.navigationBar.translucent=YES;
    [self.screenView addSubview:self.headView];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.estimatedRowHeight = 60;
    self.headView.backgroundColor = [UIColor whiteColor];
    self.tableView.rowHeight = UITableViewAutomaticDimension;
    UIButton * searchButton= [UIButton buttonWithType:UIButtonTypeCustom];
    searchButton.frame = CGRectMake(0, 0, 40, kNAVIGATIONTITLEHEIGHT);
    [searchButton setImage:[UIImage imageNamed:@"搜索"] forState:UIControlStateNormal];
    [searchButton setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    [searchButton addTarget:self action:@selector(searchButton) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.rightBarButtonItem =[[UIBarButtonItem alloc] initWithCustomView:searchButton];
    [self.tableView registerNib:[UINib nibWithNibName:@"TaskTableViewCell" bundle:nil] forCellReuseIdentifier:@"TaskTableViewCell"];
    [self.navigationController.navigationBar  setBackgroundImage:[UIImage imageWithColor:STRCOLOR(@"#3771E6")]
                                                  forBarPosition:UIBarPositionTopAttached
                                                      barMetrics:UIBarMetricsDefault];
    self.titleLabel.textColor = [UIColor whiteColor];
    self.tableView.backgroundColor = BackGroundColor;
    // Do any additional setup after loading the view from its nib.
}
- (void)searchButton{
    SearchViewController * search = [SearchViewController new];
    [self.navigationController pushViewController:search animated:YES];
}
- (void)sortingButton{
    if (self.sortView.show) {
        return;
    }
    if (self.popScreenView.show) {
        [self.popScreenView removieView];
    }
    self.sortView.frame = CGRectMake(0, kNAVIGATIONBARHEIGHT+40, kSCREEN_W, kSCREEN_H-kNAVIGATIONBARHEIGHT-40);
    self.headView.sortShowing = YES;
    [self.sortView showView];
    
}
- (void)screeningButton{
    if (self.popScreenView.show) {
        return;
    }
    if (self.sortView.show) {
        [self.sortView removieView];
    }
    if (![self.headView.screenString isEqualToString:@"状态筛选"]){
        NSArray * array = [self.headView.screenString componentsSeparatedByString:@","];
        [self.popScreenView.selectArray removeAllObjects];
        [self.popScreenView.selectArray addObjectsFromArray:array];
    }
    self.popScreenView.frame = CGRectMake(0, kNAVIGATIONBARHEIGHT+40, kSCREEN_W, kSCREEN_H-kNAVIGATIONBARHEIGHT-40);
    self.headView.screenShowing = YES;
    [self.popScreenView showView];
}
-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.topToView.constant = kNAVIGATIONBARHEIGHT;
    [self showLoading];
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        for (int i =0; i<5; i++) {
            NSString * tests = [NSString stringWithFormat:@"asda%d",i];
            [self.dataArray addObject:tests];;
        }
        [self hidloading];
        if (self.dataArray.count) {
            self.tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
        }
        [self.tableView reloadData];
    });
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 12;
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 1;
}
-(NSMutableArray *)dataArray{
    if (!_dataArray) {
        _dataArray = [NSMutableArray array];
    }
    return _dataArray;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    if (self.dataArray.count){
        [self.blackView removeFromSuperview];
    }
    else{
        [self.tableView bringSubviewToFront:self.blackView];
    }
    return self.dataArray.count;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 1;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    TaskTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"TaskTableViewCell"];
//    cell.opinionLabel.text =[NSString stringWithFormat:@"%@",self.dataArray[indexPath.section]];
    return cell;
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
